﻿using System.Linq;
using System.Xml;
using SZJT.Framework.Adapter;

namespace SZJT.Framework.Services
{
    public static class Plugin
    {
        private static string CheckValue(XmlNode node)
        {
            return null == node ? string.Empty : node.InnerXml;
        }

        public static string GetAppTaskList(string userId,string taskGuid, string dataGuid, string dataType)
        {
            var userTaskDoc= new XmlDocument();
            var userTaskStr = AdapterFactory.GetAdapter(taskGuid).GetData(taskGuid, userId, "UserTaskList").InnerXml;
            //var userTaskStr =
            //    "<Document TaskGuid='0690f841-7773-445c-9f48-a7a0427686d5' DataGuid='0690f541-7773-445c-9f48-a7a0427686d7' DataType='UserTaskList'><UserTaskList><Guid Type='TEXT'>33308fa-2c9f-47c6-80cd-ql1doo6fb6482</Guid><Name Type='TEXT'>受理</Name><AppGuid Type='TEXT'>d7075530-e121-4f65-8df8-86671213d099</AppGuid><TaskType Type='TEXT'>业务管理</TaskType><Status Type='TEXT'>true</Status></UserTaskList></Document>";
            userTaskDoc.LoadXml(userTaskStr);
            var userTaskList = userTaskDoc.DocumentElement.ChildNodes;
            var tempList = userTaskList.Cast<XmlNode>().ToList();
            foreach (var task in from XmlNode task in userTaskList
                                     let appNode = task.SelectSingleNode("AppGuid")
                                     where null != appNode
                                     where !dataGuid.Equals(appNode.InnerXml.Trim())
                                     select task)
            {
                tempList.Remove(task); 
            }
            var result=XmlNodeManage.GetDocumentNode(taskGuid, dataGuid, dataType);
            foreach (var node in tempList)
            {
                var subNode = XmlNodeManage.CreateNode("AppTaskList");
                XmlNodeManage.SetSubValue(subNode, "Guid", CheckValue(node["Guid"]));
                XmlNodeManage.SetSubValue(subNode, "Name", CheckValue(node["Name"]));
                XmlNodeManage.SetSubValue(subNode, "Status", CheckValue(node["Status"]));
                XmlNodeManage.SetSubValue(subNode, "TaskType", CheckValue(node["TaskType"]));
                XmlNodeManage.SetSubValue(subNode, "Icon", CheckValue(node["Icon"]));
                XmlNodeManage.SetSubValue(subNode, "Order", string.Empty);
                result.AppendChild(subNode);
            }
            return result.OuterXml;
        }
    }
}